'use strict';

/* Controllers */


function searchCtrl($scope,$http,$location) {
	$http.get('trendingCategories').success(function(data) {
    $scope.trendingCategories = data;
//$scope.typeahead=["data1","data2"];
  });
	
	$http.get('category').success(function(data) {
	    $scope.categories = data;
	//$scope.typeahead=["data1","data2"];
	  });
  
  
  $scope.searchFrm={};
$scope.typeaheadFn = function(query,callback) {
  var arr=[];
   $.each($scope.categories, function (i, cat) {
		if(cat.name.toLowerCase().indexOf(query.trim().toLowerCase())!=-1){
			arr.push(cat.name);
		}	
    });
	callback(arr);
};

$scope.search=function() {
		
	var id=-1;
	 $.each($scope.categories, function (i, cat) {
		if(cat.name.toLowerCase() == $scope.searchFrm.txt.trim().toLowerCase()){
			id=cat.id;
			//break;
		}	
    });
	if(id!=-1){
		$scope.typeaheadValue="";
		$location.path("/scategory/"+id);
	}
};
	
	//$location.path("/allusers");
	
}
searchCtrl.$inject = ['$scope','$http','$location'];//inject required

function searchUserCtrl($scope, $stateParams, $http) {

	$http.get('category/' + $stateParams.id).success(function(data) {
    $scope.users = data;
  });
  
  var count=10;
  var last=count;
  
  $scope.fetchMore= function(){
		$http.get('category/' + $stateParams.id).success(function(data) {
		last=last+data.length;
		$scope.users=$scope.users.concat(data);
	});
  }
  
   $scope.show=function(user){
	if(typeof user.show === 'undefined'){
		user.show=false;
	}
		return user.show;
	};
	$scope.togleProfile=function(user){
		if(user.show === false){
			user.show=true;
			if(typeof user.completeProfile === 'undefined'){
				$http.get('user/'+user.id).success(function(data){
					user.completeProfile=data;
				});
			}
		}else{
			user.show=false;
		}
	};
}
searchUserCtrl.$inject = ['$scope','$stateParams','$http'];//inject required

function allUserCtrl($scope, $http) {

	$http.get('search').success(function(data) {
		$scope.users = data;
 });
 
 $scope.show=function(user){
	if(typeof user.show === 'undefined'){
		user.show=false;
	}
		return user.show;
	};
 
	$scope.mouseover=function(user){
		user.show=true;
	};
	$scope.mouseleft=function(user){
		user.show=false;
	};
	
	
  var count=10;
  var last=count;
  
  $scope.fetchMore= function(){
		$http.get('search').success(function(data) {
		last=last+data.length;
		$scope.users=$scope.users.concat(data);
	});
  }
}
allUserCtrl.$inject = ['$scope','$http'];//inject required